const app = getApp()
Page({
  data:{
      nickName: "",
      department:"",
      studentNum:"",
      campusCardId:"",
      input_value:"",//当前输入的内容
      hide_dialog:true,
      default_value_department:[],
      default_value_grade:[],
      default_value_class:[],
      toast_value:"",
      toastType:"",
      chooseYear:"选择入学年份",
      chooseGrade:"选择层次",
      chooseClass:"选择班级",
      year:["16级","17级","18级","19级","20级","21级","22级","23级","24级","25级","26级","27级","28级","29级","30级"]
    }, 
  onLoad: function (options) {
      this.setData({nickName:app.globalData.nickName})
      wx.cloud.callFunction({name:'function',data:{type:"getDefaultValue",where:{ _id:'859059a56184aaed03dedd284c5273d1'}}}).then(res=>{
        this.setData({
          default_value_department: res.result.res.data[0].department,
          default_value_class: res.result.res.data[0].class,
          default_value_grade: res.result.res.data[0].grade,
        })
        var userinfo=res.result.user.data
        if(userinfo.length>0){
          this.setData({
            department:userinfo[0].department,
            chooseClass:userinfo[0].class,
            chooseGrade:userinfo[0].grade,
            studentNum:userinfo[0].studentId,
            campusCardId:userinfo[0].cardId,
            chooseYear:userinfo[0].year,
          })
        }
      })
  },
  bindPickerChange: function (e) {
    this.setData({ department: this.data.default_value_department[e.detail.value] })
  },bindYearPickerChange(e){
    this.setData({ chooseYear: this.data.year[e.detail.value] })
  },
  changeInput(e){
    this.setData({input_value:e.detail.value})
  },
  showDialog(e){
    this.setData({
      hide_dialog:false,
      toastType:e.currentTarget.dataset.type,
      input_value:""
    })
  },
  dialogCancel(){
    this.setData({hide_dialog:true,toast_value:""})
  },dialogConfirm(e){
      this.setData({hide_dialog:true,toast_value:""})
      if(this.data.toastType=="studentNum"){
        this.setData({studentNum:this.data.input_value})
      }
      if(this.data.toastType=="campusCardId"){
        this.setData({campusCardId:this.data.input_value})
      }
  },bindGradePickerChange(e){
    this.setData({ chooseGrade: this.data.default_value_grade[e.detail.value] })
  },bindClassPickerChange(e){
    this.setData({ chooseClass: this.data.default_value_class[e.detail.value] })
  },update(){
    if(this.data.department==""){
      wx.showToast({title: '请选择系院',image:'../../images/tips.png',duration:2000})
      return
    }
    if(this.data.studentNum==""){
      wx.showToast({title: '请输入学号',image:'../../images/tips.png',duration:2000})
      return
    }
   if(this.data.chooseYear=="选择入学年份"){
      wx.showToast({title: '请选择入学年份',image:'../../images/tips.png',duration:2000})
      return
    } if(this.data.chooseGrade=="选择层次"){
      wx.showToast({title: '请选择层次',image:'../../images/tips.png',duration:2000})
      return
    } 
     if(this.data.chooseClass=="选择班级"){
      wx.showToast({title: '请选择班级',image:'../../images/tips.png',duration:2000})
      return
    }
     
    if(this.data.campusCardId==""){
      wx.showToast({title: '请输入校园卡',image:'../../images/tips.png',duration:2000})
      return
    }
    wx.cloud.callFunction({name:'function',data:{
      type:"update_user_info",
      department:this.data.department,
      cardId:this.data.campusCardId,
      studentId:this.data.studentNum,
      class:this.data.chooseClass,
      grade:this.data.chooseGrade,
      year:this.data.chooseYear
      }}).then(res=>{
          wx.navigateBack({delta: 1})
    })
  }
})